package com.cskaoyan._04sort;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/16 11:35
 **/

public class ComparatorTest {
    public static void main(String[] args) {
        // 创建学生对象数组
        Student2 s1 = new Student2("张三", 20, 69);
        Student2 s2 = new Student2("李四", 23, 59);
        Student2 s3 = new Student2("王五", 20, 88);
        Student2 s4 = new Student2("赵六", 25, 77);
        Student2 s5 = new Student2("飞哥", 22, 100);
        // 填充数组
        Student2[] students = {s1, s2, s3, s4, s5};
        // 排序前
        System.out.println("排序前:");
        System.out.println(Arrays.toString(students));
        // 排序后
        // 使用接口的实现类
        // Arrays.sort(students, new MyComparator());

        // - 匿名内部类
        //Arrays.sort(students, new Comparator<Student2>() {
        //    @Override
        //    public int compare(Student2 o1, Student2 o2) {
        //        // 比较规则
        //        // 按照年龄从大到小排
        //        return o2.getAge() - o1.getAge();
        //    }
        //});
        //- lambda表达式
        Arrays.sort(students, (stu1, stu2) -> stu1.getScore() - stu2.getScore());

        // 输出
        System.out.println("排序后:");
        System.out.println(Arrays.toString(students));
    }

}

class Student2{
    String name;
    int age;
    int score;

    public Student2(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

}



//- 手写接口类实现

class MyComparator implements Comparator<Student2>{
    @Override
    public int compare(Student2 o1, Student2 o2) {
        // 比较规则
        // 按照年龄从小到大进行排序
        return o1.getAge() - o2.getAge();
    }

    //@Override
    //public int compare(Object o1, Object o2) {
    //    return 0;
    //}
}
